package queue.class1;

/**
 * https://leetcode.cn/problems/implement-queue-using-stacks/
 * 用栈实现队列
 */
public class Code03_MyQueue {

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.pop();
        myQueue.empty();
    }

    private static class MyQueue {

        private int[] stack1;
        private int[] stack2;
        int r1, r2;

        public MyQueue() {
            stack1 = new int[100]; // 入
            stack2 = new int[100]; // 出
            r1 = r2 = 0;
        }

        public void push(int x) {
            stack1[r1++] = x;
        }

        public int pop() {
            if (r2 == 0) {
                transfor();
            }
            return stack2[--r2];

        }

        public int peek() {
            if (r2 == 0) {
                transfor();
            }
            return stack2[r2 - 1];
        }

        private void transfor() {
            int i = r1;
            while (i > 0) {
                stack2[r2++] = stack1[--i];
            }
            r1 = 0;
        }

        public boolean empty() {
            return r1 == 0 && r2 == 0;
        }
    }
}
